rm(list = ls())
setwd("/Users/John/Dropbox/")

# --- Load Required Packages ---
library(readr)
library(interflex)
library(tidyverse)
library(ggplot2)
library(cowplot)

# --- Load Dataset ---

df <- as.data.frame(read_csv("JOP_Replication_Materials/data/processed/final_dataset.csv"))

# --- Interflex Model ---
intflex <- df %>%
  dplyr::select(isic, isic2, year, strategic, sqrtta, isic_year, median_share, 
                med_hhi_isic2, med_soe_isic2) %>%
  mutate(Strategic = strategic)

intflex <- as.data.frame(intflex)

raw <- interflex(estimator = "raw", Y = "isic_year", D = "Strategic", X = "median_share",
          Z = c("med_hhi_isic2", "med_soe_isic2"), 
          data = intflex, weights = NULL, Ylabel = "Tech Absorption Policies", Dlabel = "Strategic", 
          Xlabel = "Median Processing Share", main = "Raw Plot of Tech Abs., Strategic vs. Non-Strategic", 
          cex.main = 1.2, ncols=2, na.rm = TRUE)

raw_plot <- raw + 
  theme(plot.margin = margin(10, 20, 10, 10)) +  # Adjust right margin to avoid cut-off
  scale_x_continuous(expand = c(0, 0))

# --- Save as PDF ---
plot_path <-"JOP_Replication_Materials/appendix/output/C_figure_17.pdf"
ggsave(plot_path, raw_plot, width = 10, height = 7)

browseURL(plot_path)

